package quartz.demo1;

import java.text.ParseException;

import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.SimpleScheduleBuilder;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;
import org.quartz.impl.triggers.CronTriggerImpl;

/**
 * 创建任务调度，并执行
 * @author long    
 *
 */
public class MainScheduler {
    
    //创建调度器
    public static Scheduler getScheduler() throws SchedulerException{
        SchedulerFactory schedulerFactory = new StdSchedulerFactory();
        return schedulerFactory.getScheduler();
    }
    
    
    public static void schedulerJob() throws Exception{
        //创建任务
        JobDetail jobDetail = JobBuilder.newJob(RequestJobUrl.class).withIdentity("job1", "group1").build();
        //创建触发器 每3秒钟执行一次
//        Trigger trigger = TriggerBuilder.newTrigger().withIdentity("trigger1", "group3")
//                            .withSchedule(SimpleScheduleBuilder.simpleSchedule()
//                    		.withIntervalInSeconds(3).repeatForever())
//                            .build();
        
        CronTriggerImpl trigger = new CronTriggerImpl();// 触发器名,触发器组
		trigger.setName("job1");
		trigger.setGroup("group1");
		trigger.setCronExpression("0/5 47 9 ? * *");// 触发器时间设定
        Scheduler scheduler = getScheduler();
        //将任务及其触发器放入调度器
        scheduler.scheduleJob(jobDetail, trigger);
        //调度器开始调度任务
        scheduler.start();
        
    }
    
    public static void main(String[] args) throws Exception {
        MainScheduler mainScheduler = new MainScheduler();
        mainScheduler.schedulerJob();
    }

} 